#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit2")
HotKeySet("9", "theExit2")

call("screenshot", "start")

$numsweeps = IniRead("settings.ini", "run", "numsweeps", 1)

$stormarmor = 1 ;storm armor is slot 1
$explosion = 1 ;archon explosion is slot 1
$teleport = 2 ;teleport is slot 2
$magicweapon = 3 ;magic weapon is slot 3
$familiar = 4 ;familiar is slot 4
$goldpiles = 5 ;Number of gold piles to pick up

$firstshrine = 0
$firsthealthpool = 0
$secondshrine = 0
$secondhealthpool = 0
$legendaries = 0

While 1 
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repair") ;F4
	processkey('74', "getshrine32") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
	processkey('79', "findring") ;F10
WEnd

Func leorics()	
While 1
	Sleep($lagfactor)
	
	;Log Run Start
	call("logmessage", "Starting Run " & $runs)
	$runs = $runs + 1
	
	;Randomly take a break
	If $runs > 10 Then
		call("takebreak")
	EndIf
	
	;Repair if needed
	If call("repair3")=0 Then
		;Move to portal
		call("fuzzyclick","left", 1042, 206)
	EndIf

	;Move mouse left of ourselves
	call("fuzzymove", 994, 1078)

	Sleep(Random(4000,4500,1))
	Sleep($lagfactor)

	
	;Teleport down
	call("castSpell", $teleport)
	Sleep(Random(200,300,1))
	call("castSpell", $teleport)
	Sleep(Random(200,300,1))
	call("castSpell", $teleport)
	Sleep(Random(1200,1300,1))
	
	If getshrine31()=1 Then
		$firstshrine = $firstshrine+1
		Fight(1310,525)
	Else
		;Move to next shrine
		Send("{CTRLDOWN}")
		call("fuzzyclick","left",1868,1074)
		Sleep(Random(100,200))
		Send("{CTRLUP}")
		Sleep(Random(1800,1900))
		
		Send("{CTRLDOWN}")
		call("fuzzyclick","left",1868,1074)
		Sleep(Random(100,200))
		Send("{CTRLUP}")
		Sleep(Random(1800,1900))
		
		Send("{CTRLDOWN}")
		call("fuzzyclick","left",1337,209)
		Sleep(Random(100,200))
		Send("{CTRLUP}")
		Sleep(Random(1700,1800))
		
		If getshrine32()=1 Then
			$secondshrine = $secondshrine+1
			Fight(1245,343)
		EndIf
		
	EndIf
	
	;Pick up gold
	call("pickupGold", 1)
	
	call("exitgame")
	
	;Restart Script
WEnd	
	
EndFunc

Func FindDoor()
	;Find door
	Local $coord = PixelSearch(300, 0, 600, 100, 0x285EC5, 20)

	If Not @error Then
		MouseClick("left", $coord[0], $coord[1])
		fuzzysleep(2000 * $runspeed)
	EndIf
EndFunc

Func getshrine31()
	;Health Pool
	Local $badcolor = 0xFF1306
	Local $badarr = PixelSearch(1237,481,1367,545,$badcolor,10)
	If IsArray($badarr) Then
		$firsthealthpool = $firsthealthpool+1
		return 0
	EndIf
	
	Local $color1 = 0xEA4028
	Local $color2 = 0xA52D1C
	Local $color3 = 0x7D2B24

	Local $arr = PixelSearch(1746,183,1769,204,$color1,15)

	If IsArray($arr) Then
		;MsgBox(0,"found","found")
		return 1
	Else
		Local $arr = PixelSearch(1746,183,1769,204,$color2,15)
		If IsArray($arr) Then
			;MsgBox(0,"found","found")
			return 1
		Else
			Local $arr = PixelSearch(1746,183,1769,204,$color3,15)
			If IsArray($arr) Then
				;MsgBox(0,"found","found")
				return 1
			EndIf
		EndIf
		
		;MsgBox(0,"failed","failed")
		return 0
	EndIf
EndFunc

Func getshrine32()
	;Health Pool
	Local $badcolor = 0xFF1306
	Local $badarr = PixelSearch(1266,40,1550,450,$badcolor,10)
	If IsArray($badarr) Then
		$secondhealthpool = $secondhealthpool+1
		return 0
	EndIf
	
	Local $color1 = 0xEA4028
	Local $color2 = 0xA52D1C
	Local $color3 = 0x7D2B24
	Local $color4 = 0xD33A25
	Local $color5 = 0xB43220

	Local $arr1 = PixelSearch(1747,171,1770,195,$color1,15)
	Local $arr2 = PixelSearch(1747,171,1770,195,$color2,15)
	Local $arr3 = PixelSearch(1747,171,1770,195,$color3,15)
	Local $arr4 = PixelSearch(1747,171,1770,195,$color4,15)
	Local $arr5 = PixelSearch(1747,171,1770,195,$color5,15)

	If IsArray($arr1) Then
		;MsgBox(0,"found","found")
		return 1
	EndIf
	
	If IsArray($arr2) Then
		;MsgBox(0,"found","found")
		return 1
	EndIf
	
	If IsArray($arr3) Then
		;MsgBox(0,"found","found")
		return 1
	EndIf
	
	If IsArray($arr4) Then
		;MsgBox(0,"found","found")
		return 1
	EndIf
	
	If IsArray($arr5) Then
		;MsgBox(0,"found","found")
		return 1
	EndIf

	;MsgBox(0,"failed","failed")
	Return 0
EndFunc

Func Fight($x, $y)
	;Buff
	Local $mode = Random(1,6,1)
	If $mode = 1 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 2 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	ElseIf $mode = 3 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 4 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	ElseIf $mode = 5 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	Else
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	EndIf
		
	;Click Shrine
	call("fuzzyclick", "left", $x, $y)
	Sleep(Random(1000,1200,1))
	;Fix weird bug where shrine is too far
	If $y = 343 Then
		call("fuzzyclick", "left", 1155, 385)
		Sleep(Random(600,1000,1))
	EndIf
		
	;Fire left
	call("fuzzyclick", "right", 881,482)
	Send("{SHIFTDOWN}")
	MouseDown("left")
	
	
	For $i=0 to $numsweeps step 1
		call("fuzzymove", Random(834,1108,1),Random(367,595,1))
		call("fuzzysleep", Random(0,200,1))
		call("castSpell", $explosion)
		call("fuzzysleep", Random(2000,2200,1))
	Next
	
	
	MouseUp("left")
	Sleep(Random(100,400,1))
	Send("{SHIFTUP}")
	call("fuzzysleep", Random(1400,1800,1))

	;Pick up Legendaries
	call("getLegendaries", 0)
	
	If ($statusLegendaryDrop = 1) Then
		$legendaries = $legendaries + 1
	EndIf
	
	call("getGems", 0)
EndFunc

Func theExit2()
	call("logmessage", "First Shrine " & $firstshrine)
	call("logmessage", "First Health Pool " & $firsthealthpool)
	call("logmessage", "Second Shrine " & $secondshrine)
	call("logmessage", "Second Health Pool " & $secondhealthpool)
	call("logmessage", "Legendaries " & $legendaries)
	call("logmessage", "Total Shrines " & $firstshrine + $secondshrine)
	call("logmessage", "Shrine% " & ((($firstshrine + $secondshrine)/($runs-1))*100))
    Exit
EndFunc